package de.fhd.pms;

import java.awt.BorderLayout;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.image.ImageProducer;
import java.awt.image.RenderedImage;
import java.io.*; 
import java.net.*; 
import java.util.Calendar;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;


public class ReadBytes {     
	public static void main( String [] args ) throws IOException {          
		//URL url = new URL("http://www.google.com/intl/en_ALL/images/logo.gif");
		URL url = new URL("http://192.168.178.50/test.jpg");              // Read the image ...         
		
		
		// Url to BufferImage
		BufferedImage image = null;
		try
		   {
		   image = ImageIO.read( url );
		   }
		catch ( IOException e )
		   {
		   System.out.println( "image missing" );
		   }
		



		BufferedImage bufferedImage = new BufferedImage (640,480,BufferedImage.TYPE_BYTE_BINARY);
		//bufferedImage.createGraphics().drawImage( image, 0, 0, this /* observer */ );
		
		// Bufferimage to byte
		ByteArrayOutputStream baos = new ByteArrayOutputStream(921600 );
		ImageIO.write( image, "jpeg" , baos );
		baos.flush();
		byte[] resultImageAsRawBytes = baos.toByteArray();
		baos.close();
		
		
		
		InputStream inputStream      = url.openStream();         
		ByteArrayOutputStream output = new ByteArrayOutputStream();        
		byte [] buffer               = new byte[921600];   // 921600       
		int n = 0;         
		
		while (-1 != (n = inputStream.read(buffer))) {
			output.write(buffer, 0, n);         }  
		
		inputStream.close();          // Here's the content of the image...         
		
		byte [] data = output.toByteArray();          // Write it to a file just to compare...         
		
		OutputStream out = new FileOutputStream("data.jpg");         
		
		out.write( data );         
		out.close();          // Print it to stdout          
		
		for( byte b : data ) {                 
			//System.out.printf("0x%x ", b);         
		}  
	
		
		      

		  
		     // Get the image ==============================================================
		     java.awt.Image image1 = ImageIO.read ( url ) ; //java.awt.Toolkit.getDefaultToolkit (  ) .createImage ( url ) ; 
		     int w = image1.getWidth ( null ) ; 
		     int h = image1.getHeight ( null ) ; 
		  
		  
		     BufferedImage bi = new BufferedImage ( w, h, BufferedImage.TYPE_BYTE_GRAY) ; 
		     Graphics2D g2 = bi.createGraphics (  ) ; 
		     g2.drawImage ( image1, 0, 0, null ) ; 
		     g2.dispose (  ) ; 
		     g2 = null; 
		     ImageIO.write ( bi, "jpg", new File ( "output.jpg" )  ) ;

		
		    // URL u = new URL( url );
		     Toolkit tk = Toolkit.getDefaultToolkit();
		     tk.createImage( (ImageProducer)url.getContent() );
		     
		     
		  // Use a label to display the image
		     JFrame frame = new JFrame();
		     JLabel label = new JLabel(new ImageIcon(bi));
		     frame.getContentPane().add(label, BorderLayout.CENTER);
		     frame.pack();
		     frame.setVisible(true);

		     
		     
//		      Image test = null;
//		      Toolkit tk1 = Toolkit.getDefaultToolkit();
//              test = tk1.getImage(url);
//              //File outputFile = new File("hjw"+Calendar.getInstance().getTimeInMillis()+".jpg"); 
//              File outputFile = new File("hjw.jpg"); 
//              
//              BufferedImage bufferedImage3 = new BufferedImage(640,480, BufferedImage.TYPE_INT_BGR);
//              //Graphics2D g3 = bufferedImage3.createGraphics();
//              //g3.drawImage(, null, null);   
//              ImageIO.write(bufferedImage3, "jpg", outputFile); 
//		    
		

		
		System.out.printf("size " + resultImageAsRawBytes.length + " w: " + w +"h: " + h );
	} 



}